package com.zjff.mobiletwo.controller;

import com.zjff.mobiletwo.entity.UserEntity;
import com.zjff.mobiletwo.service.TextService;
import com.zjff.mobiletwo.service.TokenService;
import com.zjff.mobiletwo.service.util.TextUtils;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin//跨域
@RestController//遵循RestFul
@RequestMapping("v2/user")
public class TestController {
    @Resource
    TextService textService;
    @Resource
    TokenService tokenService;

    @RequestMapping(value = "/login")
    public Object login(@RequestParam(value = "username")String username,
                        @RequestParam(value = "password")String password){
        Map<String,Object> map=new HashMap<>();
        if (TextUtils.Isempty(username) ||TextUtils.Isempty(password)){
            map.put("code",100);
            map.put("msg","请求不能为空");
        }else {
            UserEntity userEntity= textService.login(username);
            if(userEntity!=null){
                if (password.equals(userEntity.getPassword())){
                    String token = tokenService.getToken(userEntity);
                    map.put("code",200);
                    map.put("msg","登入成功");
                    map.put("token",token);
                    map.put("userEntity",userEntity);
                }else {
                    map.put("code",101);
                    map.put("msg","用户名或者密码错误");
                }
            }else {
                map.put("code",102);
                map.put("msg","不存在该用户");
            }
        }
        return map;
    }
}
